/** 模式定义 */
var mongoose = require('mongoose')
var Schema = mongoose.Schema

var MovieSchema = new Schema({
    doctor: String,
    title: String,
    country: String,
    year: Number,
    poster: String,
    language: String,
    flash: String,
    summary: String,
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updateAt: {
            type: Date,
            default: Date.now()
        }
    }
})

MovieSchema.pre('save', function (next) {
    if (this.isNew) {
        this.meta.createdAt = this.meta.updateAt = Date.now()
    } else {
        this.meta.updateAt = Date.now()
    }
    next()
})

// 添加静态方法
MovieSchema.statics = {
    // 获取数据库所有数据
    fetch: function (cb) {
        return this
            .find({})
            .sort('meta.updateAt').
        exec(cb) //调取回调方法
    },
    // 查询单条数据
    findById: function (id, cb) {
        return this
            .findOne({
                _id: id
            }).
        exec(cb) //调取回调方法
    }
}

module.exports = MovieSchema